interface QuerySessionClass {
  key?: string;
  getQuerySession: () => void;
  setQuerySession: (query: any) => void;
}

export default class QuerySession<QuerySessionClass> {
  key: string | undefined;
  constructor(key?: string) {
    this.key = key || this.generateKey();
  }

  getQuerySession() {
    const str = window.sessionStorage.getItem(this.key!);
    if (str) {
      setTimeout(() => {
        window.sessionStorage.removeItem(this.key!);
      }, 1000);
      return JSON.parse(str);
    }
  }

  setQuerySession(query: any) {
    if (typeof query !== "object") return;
    if (!this.key) this.generateKey();
    window.sessionStorage.setItem(this.key!, JSON.stringify(query));
  }

  generateKey() {
    if (this.key) return "";
    this.key = "__" + window.location.pathname;
  }
}
